package com.ruoyi.equipment.controller;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.equipment.domain.TUserLog;
import com.ruoyi.equipment.service.ITUserLogService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 设备日志Controller
 *
 * @author ruoyi
 * @date 2021-12-27
 */
@RestController
@RequestMapping("/facility/userLog")
public class TUserLogController extends BaseController {
    @Autowired
    private ITUserLogService tUserLogService;

    /**
     * 查询设备日志列表
     */
    @PreAuthorize("@ss.hasPermi('facility:userLog:list')")
    @GetMapping("/list")
    public TableDataInfo list(TUserLog tUserLog) {
        startPage();
        Long userId = getUserId();
        tUserLog.setUserId(userId.intValue());
        List<TUserLog> list = tUserLogService.selectTUserLogList(tUserLog);
        return getDataTable(list);
    }

    /**
     * 导出设备日志列表
     */
    @PreAuthorize("@ss.hasPermi('facility:userLog:export')")
    @Log(title = "设备日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TUserLog tUserLog) {
        List<TUserLog> list = tUserLogService.selectTUserLogList(tUserLog);
        ExcelUtil<TUserLog> util = new ExcelUtil<TUserLog>(TUserLog.class);
        util.exportExcel(response, list, "设备日志数据");
    }

    /**
     * 获取设备日志详细信息
     */
    @PreAuthorize("@ss.hasPermi('facility:userLog:query')")
    @GetMapping(value = "/{userLogId}")
    public AjaxResult getInfo(@PathVariable("userLogId") Integer userLogId) {
        return AjaxResult.success(tUserLogService.selectTUserLogByUserId(userLogId));
    }


    //设备归还的信息
    @PostMapping("/returnDrawer")
    @PreAuthorize("@ss.hasPermi('facility:userLog:edit')")
    public AjaxResult returnDrawer(@RequestBody TUserLog tUserLog){
        TUserLog tUserLogVo = tUserLogService.selectTUserLogByUserId(tUserLog.getUserLogId());
        Integer drawerStu = tUserLogVo.getDrawerStu();
        if (drawerStu==1){
            return AjaxResult.error("补充信息失败'" + tUserLogVo.getDrawerStu() + "'失败，仓匣还未归还");
        }
        tUserLogVo.setRemark(tUserLog.getRemark());
        tUserLogVo.setImgUrl(tUserLog.getImgUrl());
        return AjaxResult.success(tUserLogService.returnDrawer(tUserLogVo));
    }

    @GetMapping(value = "/getUserLog/{drawerId}")
    public AjaxResult getUserLog(@PathVariable("drawerId")String drawerId){
      SysUser user =  tUserLogService.getUserLogByDrawerId(drawerId);
      return AjaxResult.success(user);
    }

}
